package com.agua.enturno.vista.modelos;

import org.apache.wicket.model.IModel;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;


public class DateModel implements IModel<String> {
	
	private static final String DATE_FORMAT = "dd/MM/yyyy";

	private static final long serialVersionUID = 1L;
	
	private IModel<DateTime> dateTimeModel;
	
	public DateModel(IModel<DateTime> dateTimeModel) {
		this.dateTimeModel = dateTimeModel;
	}

	@Override
	public void detach() {
		dateTimeModel.detach();
	}

	@Override
	public String getObject() {
		return dateTimeModel.getObject().toLocalDate().toString(DATE_FORMAT);
	}

	@Override
	public void setObject(String object) {
		
		LocalDate localDate = LocalDate.parse(object, DateTimeFormat.forPattern(DATE_FORMAT));
		this.dateTimeModel.setObject(this.dateTimeModel.getObject().withDate(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth()));
	}
	
}
